package com.sulent.frame.mp.config;

/**
 * @ClassName AutoWriteConfig
 * @Description: TODO
 * @Author Submerge.
 * @Since 2023/2/27 14:47
 * @Version 1.0
 */
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @ClassName MyMetaObjectHandler
 * @Description: 自定义元数据对象处理器, 并交由Spring管理：统一处理公共字段自动填充
 * @Author Submerge.
 * @Since 2023/2/27 14:47
 * @Version 1.0
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    private static final String CURRENT_USER_ID = "0123456789";

    /**
     * 对公共字段统一添加处理
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("delFlag",0);

        metaObject.setValue("createBy", CURRENT_USER_ID);
        metaObject.setValue("updateBy", CURRENT_USER_ID);

    }

    /**
     * 对公共字段统一修改处理
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateBy", CURRENT_USER_ID);
    }
}
